package com.xpec.c4.game.login;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.player.Player;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.ResultNo;
import com.xpec.c4.game.datamanager.agent.PlayerData;
import com.xpec.c4.game.datamanager.agent.PlayerDataAgent;
import com.xpec.c4.game.login.async.AccountLoginRequestGetAccountData;
import com.xpec.c4.game.login.async.CharacterLoginRequestGetPlayer;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.module.BasicServiceModule;


/**
 * LoginModule使用的版本
 * @author brian_yang
 *
 */
public class PlayerDataAgentLogin extends PlayerDataAgent {

	/**
	 * 
	 */
	private static final Log log = LogFactory.getLog(PlayerDataAgentLogin.class);
	
	/**
	 * 
	 * @param module
	 */
	public PlayerDataAgentLogin(BasicServiceModule module) {
		super(module);
	}

	/**
	 * 
	 * @param guid
	 * @param charGuidList
	 * @return
	 */
	public boolean getPlayerListFromDC(GUID accountID,
			AccountLoginRequestGetAccountData accData, Object... userData) {
		
		BaseDataInfo data = new BaseDataInfo();
		data.setKey(accountID);
		data.setData(accData);
		
		data.setMessageType(MessageTypes.GET_PLAYER_LIST_BY_GUID);
		
		// 產生callback需要的method
		Method mth = getCallbackMethod(this, "getPlayerListCallback");
		
		return super.dataRequest(this, null, null, this, mth, data, userData);
	}
	
	/**
	 * 
	 * @param obj
	 * @param userObject
	 */
	public void getPlayerListCallback(BaseDataInfo obj, Object... userObject){
		
		AccountLoginRequestGetAccountData tmp = (AccountLoginRequestGetAccountData)obj.getData();
		
		if(tmp.msg != null && getModule() != null){
				
			GUID lastLockPlayerGUID = null;
			String deviceToken = null;
			if(userObject[1] != null){
				//取出deviceToken夾著有效時間
				String tokenWithExp = (String) userObject[1];
				String[] checkString = tokenWithExp.split(",");
				
				//如果還在有效時間之內才要告訴client
				if(TimeSys.currentTimeSec() < Long.valueOf(checkString[1])){
					
					//取出最後鎖定角色
					if(userObject[0] != null){
						String guidString = (String) userObject[0];
						lastLockPlayerGUID = GUID.parseUUID(guidString);
					}
					
					//如果不是空字串就放進去devicToken(所以如果是空字串就會回傳null)
					if(!checkString[0].equals(""))
						deviceToken = checkString[0];
				}
			}
				
			//放進回應中
			tmp.msg.lastLockPlayerGUID = lastLockPlayerGUID;
			tmp.msg.deviceToken = deviceToken;
			((LoginModule)getModule()).accountLoginRequestLoadPlayerCallback(tmp);
		}
	}
	
	/**
	 * 
	 * @param obj
	 * @param userObject
	 */
	public void CharacterLoginRequestGetPlayerCallback(BaseDataInfo obj, Object... userObject){
		
		super.queryPlayerDataCallback(obj, userObject);
		
		switch(obj.getResultCode()){

		case ResultNo.OK: {
			PlayerData q = (PlayerData) obj;
			if (userObject != null && userObject[0] != null
					&& userObject[0] instanceof CharacterLoginRequestGetPlayer) {

				CharacterLoginRequestGetPlayer info = ((CharacterLoginRequestGetPlayer) userObject[0]);
				
				//把sessionID填入Player以供登入進大廳之前的流程參考2017/05/31
				Player player = (Player) q.getData();
				player.setSessionID(info.sessionID);
				info.player = player;
				
				info.ownerModule = q.getOwnerID();
				info.result = true;
				((LoginModule) getModule()).characterLoginRequestGetPlayerCallback(info);
			}
			break;
		}
			
		case ResultNo.ACCESS_DENY:
			log.info("Access deny  id: " + obj.getKey());
			break;
			
		case ResultNo.FAILED:
			log.info("Query failed  id: " + obj.getKey());
			break;
			
		case ResultNo.DATA_NOT_FOUND:
			log.info("Data not found id: " + obj.getKey());
			break;
			
		case ResultNo.DB_OPERATION_FAILED :
			log.info("DB operation failed id: " + obj.getKey());
			break;
			
		}
		
	}
	
	/**
	 * 
	 * @param str
	 * @return
	 */
	public boolean createPlayerDataToDC(GUID str, Object player, String clientIp, Object... userData) {

		PlayerData data = new PlayerData();

		data.setKey(str);
		data.setData(player);
		data.setClientIp(clientIp);
		data.setMessageType(MessageTypes.CREATE_PLAYER_INFO);
		
		// 產生callback需要的method
		Method mth = getCallbackMethod(this, "createPlayerDataCallback");
		
		return super.dataRequest(this, null, null, this, mth, data, player, userData);
	}
	
}
